﻿
@{
    ViewData["Title"] = "FormManage";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<html>
    <head>
        <meta charset="utf-8"
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
    <div>
        <table style="float:left;margin-left:100px;margin-bottom:50px;margin-top:50px">
            <tr>
                <td>
                    <div class="layui-form layui-row">
                        <label class="layui-form-label">表单名称:</label>
                        <div class="layui-col-md3" style="width:200px">
                            <input type="text" name="FormName" id="FormName" placeholder="请输入" class="layui-input">
                        </div>
                    </div>
                </td>
                <td style="margin-left:100px">
                    <button type="button" class="layui-btn layui-btn-primary" id="searchBtn" style="background-color:#538EBB;color:aliceblue"><i class="layui-icon"></i> 搜 索</button>
                    <button type="button" class="layui-btn layui-btn-primary" style="background-color:#EE9800"><i class="layui-icon"></i>重置</button>
                    <button type="button" class="layui-btn layui-btn-primary" lay-on="test-page" id="onAddinfo" style="background-color:green;color:black">添加</button>
                </td>
            </tr>
        </table>
        <table class="layui-hide" id="test" lay-filter="test"></table>
        <div id="demo-laypage-all" style="margin-left:200px"></div>
        <!--添加的弹出层-->
        <div id="add-main" style="display:none">
            <form class="layui-form" action="" style="align-content:center" onsubmit="return false">
                <div class="layui-form-item">
                    <label class="layui-form-label">表单名称</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <input type="text" id="FormName" name="FormName" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">表单描述</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <input type="text" id="FromDesc" name="FromDesc" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择行业</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <select id="TypeName" name="TypeName" lay-filter="eventtype">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择大类</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <select id="TypeFther" name="TypeFther" lay-filter="eventbig">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择小类</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <select id="TypeSun" name="TypeSun" lay-filter="aihao">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-btn-container" style="text-align:center">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">保存并设置</button>
                </div>
            </form>
        </div>
    </div>
    <script type="text/html" id="barDemo">
        <div class="layui-clear-space">
                <a style="color:dodgerblue;margin-right:15px"  lay-event="edit">编辑</a>
                <a style="color:dodgerblue" lay-event="delete">删除</a>&emsp;
        </div>
    </script>
@*     <script src="~/js/Form/FormAdd.js?n=" + new Date()"></script>
    <script src="~/js/Form/FormList.js?n=" + new Date()"></script> *@

    <script>
        layui.use(['form', 'laydate', 'util', 'jquery', 'layer', 'table', 'tree'], function () {
            var table = layui.table;
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var $ = layui.jquery;
            var laypage = layui.laypage;
            var tree = layui.tree;
            var pageSize = 10;
            var i = 0;
            //弹出层
            $('#onAddinfo').on('click', function () {
                // 批量事件
                layer.open({
                    type: 1, // page 层类型
                    area: ['1200px', '800px'],
                    title: '新增事件类型',
                    shade: 0,//控制遮罩
                    shadeClose: true, // 点击遮罩区域，关闭弹层
                    maxmin: true, // 允许全屏最小化
                    anim: 0, // 0-6 的动画形式，-1 不开启
                    content: $("#add-main"),
                });
            });
            $(function () {
                EventSelect();
            })
            function EventSelect() {
                $.ajax({
                    url: "http://8.140.245.229:8000/routechen/api/Form/HandleFormSelect?PId=0",
                    async: false,
                    type: "get",
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    success: function (res) {
                        var select = $("#TypeName");
                        $.each(res, function (index, item) {
                            select.append($('<option>').val(item.EventTypeId).text(item.EventTypeName))
                        })
                        layui.form.render('select');
                        form.on('select(eventtype)', function (data) {
                            var value = data.value; // 获得被选中的值
                            $.ajax({
                                url: "http://8.140.245.229:8000/routechen/api/Form/HandleFormBigClass",
                                data: {
                                    PId: value
                                },
                                type: "get",
                                headers: {
                                    'Authorization': "Bearer " + localStorage["token"] + ""
                                },
                                success: function (res) {
                                    var select = $("#TypeFther");
                                    $("#TypeFther").empty();
                                    select.append($('<option>').val(0).text("请选择"))
                                    $.each(res, function (index, item) {
                                        select.append($('<option>').val(item.EventTypeName).text(item.EventTypeName))
                                    })
                                    layui.form.render('select');
                                    form.on('select(eventbig)', function (data) {
                                        var value = data.value; // 获得被选中的值
                                        $.ajax({
                                            url: "http://8.140.245.229:8000/routechen/api/Form/HandleSmallClass",
                                            data: {
                                                PId: value
                                            },
                                            type: "get",
                                            headers: {
                                                'Authorization': "Bearer " + localStorage["token"] + ""
                                            },
                                            success: function (res) {
                                                var select = $("#TypeSun");
                                                $("#TypeSun").empty();
                                                $.each(res, function (index, item) {
                                                    select.append($('<option>').val(item.TypeName).text(item.TypeName))
                                                })
                                                layui.form.render('select');
                                            }
                                        })
                                    })
                                }
                            })
                        });
                    }
                })
            }
            function AddLogInfo(OperationalContext, PreOperation, AfterOperation) {
                const data = {
                    olContext: OperationalContext,
                    PreOperation: PreOperation,
                    AfterOperation: AfterOperation
                };
                // alert(JSON.stringify(data));
                $.ajax({
                    url: "http://8.140.245.229:8000/WorkBench/api/Log/AddLog",
                    type: 'post',
                    dataType: 'json',
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    contentType: 'application/json', // 添加正确的 Content-Type 头部
                    data: JSON.stringify(data),
                    success: function (res) {
                        // alert(res)
                        getListData(1);
                        console.log(res)
                    }
                })
            }

            form.on('submit(demo1)', function (data) {
                var obj = {};
                var field = data.field;
                obj.FormName = field.FormName;
                obj.FromDesc = field.FromDesc;
                obj.TypeName = field.TypeName;
                obj.TypeFther = field.TypeFther;
                obj.TypeSun = field.TypeSun;
                obj.CreateBy = field.CreateBy;
                obj.CreateTime = field.CreateTime;
                obj.UpdateBy = null;
                obj.UpdateTime = null;
                obj.IsDel = false;
                $.ajax({
                    url: "http://8.140.245.229:8000/webroute/api/Setting/HandleAddForm",
                    type: "post",
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    data: JSON.stringify(obj),
                    contentType: "application/json",
                    success: res => {
                        AddLogInfo("向表单管理添加数据", "", JSON.stringify(obj))
                        if (res > 0) {
                            layer.confirm('添加成功', function (index) {
                                ShowInfo(1);
                            });

                        } else {
                            layer.alert("添加失败");
                        }
                    }
                })
                return false; // 阻止默认 form 跳转
            })
            $(function () {
                ShowInfo(1);
            });
            // 单删除

            //第一个参数为操作内容，例如：登录了系统，修改/删除/添加/查询了日志表（或其他表，每次调用根据进行的操作写）
            //第二个参数为修改专用，将修改前的参数转为json字符串格式传入
            //第三个参数是将添加或删除的数据转为json传入，若是修改方法则把修改后的数据转为json字符串传入
            function AddLogInfo(OperationalContext, PreOperation, AfterOperation) {
                const data = {
                    olContext: OperationalContext,
                    PreOperation: PreOperation,
                    AfterOperation: AfterOperation
                };
                // alert(JSON.stringify(data));
                $.ajax({
                    url: "http://8.140.245.229:8000/WorkBench/api/Log/AddLog",
                    type: 'post',
                    dataType: 'json',
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    contentType: 'application/json', // 添加正确的 Content-Type 头部
                    data: JSON.stringify(data),
                    success: function (res) {
                        // alert(res)
                        //getListData(1);
                        console.log(res)
                    }
                })
            }
            function deleteData(id) {
                $.ajax({
                    url: `http://8.140.245.229:8000/webroute/api/Setting/HandleDelForm?Id=${id}`,
                    type: 'delete',
                    success: function (res) {
                        if (res.data != 0) {
                            layer.msg('删除成功');
                            ShowInfo(1);
                        } else {
                            layer.msg('删除失败');
                        }
                    },
                    error: function () {
                        layer.msg('删除请求失败');
                    }
                });
            }
            // 监听工具条，实现删除功能
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                if (obj.event == 'delete') {
                    layer.confirm('确定删除吗？', function (index) {
                        deleteData(data.FormId); // 传递正确的数据项ID
                        layer.close(index);
                    });
                } else if (obj.event == 'edit') {
                    location.href = `/Event/EventEdit?id=${data.EventId}`;
                }

            });
            //渲染
            laydate.render({
                elem: '#storageDate'
            });
            function ShowInfo(pageIndex) {
                $.ajax({
                    url: "http://8.140.245.229:8000/routechen/api/Form/HandleFormList",
                    type: 'get',
                    dataType: 'json',
                    data: {
                        FormName: $("#FormName").val(),
                        pageIndex: pageIndex,
                        pageSize: pageSize,
                    },
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    success: function (res) {
                        AddLogInfo("查询表单管理");
                        table.render({
                            elem: '#test',
                            cols: [[
                                {
                                    type: 'checkbox', align: 'center',
                                },
                                {
                                    title: '序号', align: 'center',
                                    templet: function (data) { return ++i + ((pageIndex - 1) * pageSize) }
                                },
                                { field: 'FormName', title: '表单名称', align: 'center' },
                                { field: 'FromDesc', title: '表单描述', align: 'center' },
                                { title: '操作', width: 150, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true }
                            ]],
                            //style: "width:500px;height:500px;",
                            width: 1300,
                            height: 600,
                            data: res.Datas,
                        });
                        laypage.render({
                            //挂载的分页id
                            elem: 'demo-laypage-all',
                            count: res.TotalCount,
                            limit: pageSize,
                            curr: pageIndex,
                            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                            jump: function (obj, first) {
                                console.log(obj);
                            }
                        })
                    }
                })
            }
            $("#searchBtn").on('click', function () {
                ShowInfo(1);
            })
        })
    </script>
    </body>
</html>